import os
import pygame


class AssetManager:
    def __init__(self, assets_dir=None):
        base = os.path.dirname(os.path.abspath(__file__))
        self.mole_surface = None
        self.hole_surface = None
        self._create_surfaces()

    def _create_surfaces(self):
        """创建地鼠和洞口的 Surface 对象（128x128 像素）"""
        size = (128, 128)
        # 创建洞口
        self.hole_surface = pygame.Surface(size, pygame.SRCALPHA)
        cx, cy = size[0] // 2, size[1] // 2
        # 外圈（深棕色边缘）
        pygame.draw.circle(self.hole_surface, (60, 40, 20), (cx, cy), 60)
        # 内圈（深色洞口）
        pygame.draw.circle(self.hole_surface, (30, 20, 10), (cx, cy), 50)

        # create base mole shapes for different types
        # default brown base used as fallback
        self.mole_base = pygame.Surface(size, pygame.SRCALPHA)
        pygame.draw.circle(self.mole_base, (139, 69, 19), (cx, cy + 10), 40)
        pygame.draw.circle(self.mole_base, (160, 82, 45), (cx, cy - 12), 35)
        pygame.draw.ellipse(self.mole_base, (255, 160, 160), (cx - 8, cy - 8, 16, 12))

        # Normal/open and hit variants (brown)
        self.mole_open = self.mole_base.copy()
        pygame.draw.circle(self.mole_open, (255, 255, 255), (cx - 15, cy - 15), 10)
        pygame.draw.circle(self.mole_open, (255, 255, 255), (cx + 15, cy - 15), 10)
        pygame.draw.circle(self.mole_open, (0, 0, 0), (cx - 13, cy - 15), 5)
        pygame.draw.circle(self.mole_open, (0, 0, 0), (cx + 13, cy - 15), 5)

        self.mole_hit = self.mole_base.copy()
        pygame.draw.line(self.mole_hit, (0, 0, 0), (cx - 22, cy - 15), (cx - 8, cy - 15), 3)
        pygame.draw.line(self.mole_hit, (0, 0, 0), (cx + 8, cy - 15), (cx + 22, cy - 15), 3)
        pygame.draw.ellipse(self.mole_hit, (255, 80, 120), (cx - 8, cy + 2, 16, 12))

        # Pink mole (healer) - pink face, smile, heart on head
        self.mole_pink_open = pygame.Surface(size, pygame.SRCALPHA)
        # pink body
        pygame.draw.circle(self.mole_pink_open, (255, 182, 193), (cx, cy + 10), 40)
        pygame.draw.circle(self.mole_pink_open, (255, 192, 203), (cx, cy - 12), 35)
        pygame.draw.ellipse(self.mole_pink_open, (255, 160, 160), (cx - 8, cy - 8, 16, 12))
        # smile
        pygame.draw.arc(self.mole_pink_open, (150, 0, 50), (cx - 14, cy - 8, 28, 20), 3.14, 0, 3)
        # heart on head (simple triangle-ish)
        pygame.draw.polygon(self.mole_pink_open, (220, 20, 60), [(cx, cy - 46), (cx - 8, cy - 34), (cx + 8, cy - 34)])

        self.mole_pink_hit = self.mole_pink_open.copy()
        pygame.draw.line(self.mole_pink_hit, (0, 0, 0), (cx - 22, cy - 15), (cx - 8, cy - 15), 3)
        pygame.draw.line(self.mole_pink_hit, (0, 0, 0), (cx + 8, cy - 15), (cx + 22, cy - 15), 3)
        pygame.draw.ellipse(self.mole_pink_hit, (255, 80, 120), (cx - 8, cy + 2, 16, 12))

        # Black (angry) mole
        self.mole_black_open = pygame.Surface(size, pygame.SRCALPHA)
        pygame.draw.circle(self.mole_black_open, (35, 35, 35), (cx, cy + 10), 40)
        pygame.draw.circle(self.mole_black_open, (50, 50, 50), (cx, cy - 12), 35)
        pygame.draw.ellipse(self.mole_black_open, (80, 40, 40), (cx - 8, cy - 8, 16, 12))
        # angry eyes (slanted white shapes)
        pygame.draw.polygon(self.mole_black_open, (255, 255, 255), [(cx - 22, cy - 20), (cx - 6, cy - 24), (cx - 10, cy - 12)])
        pygame.draw.polygon(self.mole_black_open, (255, 255, 255), [(cx + 22, cy - 20), (cx + 6, cy - 24), (cx + 10, cy - 12)])
        pygame.draw.polygon(self.mole_black_open, (0, 0, 0), [(cx - 18, cy - 20), (cx - 8, cy - 20), (cx - 12, cy - 14)])
        pygame.draw.polygon(self.mole_black_open, (0, 0, 0), [(cx + 18, cy - 20), (cx + 8, cy - 20), (cx + 12, cy - 14)])

        self.mole_black_hit = self.mole_black_open.copy()
        # for hit, show closed angry eyes as slashes
        pygame.draw.line(self.mole_black_hit, (255, 0, 0), (cx - 22, cy - 15), (cx - 8, cy - 11), 4)
        pygame.draw.line(self.mole_black_hit, (255, 0, 0), (cx + 8, cy - 11), (cx + 22, cy - 15), 4)

        # Gold mole
        self.mole_gold_open = pygame.Surface(size, pygame.SRCALPHA)
        pygame.draw.circle(self.mole_gold_open, (212, 175, 55), (cx, cy + 10), 40)
        pygame.draw.circle(self.mole_gold_open, (230, 190, 80), (cx, cy - 12), 35)
        pygame.draw.ellipse(self.mole_gold_open, (255, 220, 150), (cx - 8, cy - 8, 16, 12))
        pygame.draw.circle(self.mole_gold_open, (255, 255, 255), (cx - 15, cy - 15), 8)
        pygame.draw.circle(self.mole_gold_open, (255, 255, 255), (cx + 15, cy - 15), 8)
        pygame.draw.circle(self.mole_gold_open, (0, 0, 0), (cx - 14, cy - 15), 4)
        pygame.draw.circle(self.mole_gold_open, (0, 0, 0), (cx + 14, cy - 15), 4)

        self.mole_gold_hit = self.mole_gold_open.copy()
        pygame.draw.line(self.mole_gold_hit, (0, 0, 0), (cx - 22, cy - 15), (cx - 8, cy - 15), 3)
        pygame.draw.line(self.mole_gold_hit, (0, 0, 0), (cx + 8, cy - 15), (cx + 22, cy - 15), 3)

        # small UI elements: heart and +5 and boom text
        self.heart_surf = pygame.Surface((24, 24), pygame.SRCALPHA)
        pygame.draw.polygon(self.heart_surf, (220, 20, 60), [(12, 0), (4, 8), (12, 20), (20, 8)])

        self.plus5_surf = pygame.Surface((40, 24), pygame.SRCALPHA)
        f = pygame.font.SysFont(None, 24)
        txt = f.render("+5", True, (255, 215, 0))
        self.plus5_surf.blit(txt, (0, 0))

        self.boom_surf = pygame.Surface((80, 36), pygame.SRCALPHA)
        bf = pygame.font.SysFont(None, 32)
        bt = bf.render("BOOM", True, (255, 100, 0))
        self.boom_surf.blit(bt, (0, 0))

    def draw_mole(self, surface, pos, scale=1.0, hit=False, mole_type='normal'):
        """Draw a mole of given type at pos. mole_type in ('normal','pink','black','gold')."""
        if mole_type == 'pink':
            src = self.mole_pink_hit if hit else self.mole_pink_open
        elif mole_type == 'black':
            src = self.mole_black_hit if hit else self.mole_black_open
        elif mole_type == 'gold':
            src = self.mole_gold_hit if hit else self.mole_gold_open
        else:
            src = self.mole_hit if hit else self.mole_open

        if scale != 1.0:
            w = int(src.get_width() * scale)
            h = int(src.get_height() * scale)
            scaled = pygame.transform.smoothscale(src, (w, h))
            x = pos[0] - w // 2
            y = pos[1] - h // 2
            surface.blit(scaled, (x, y))
        else:
            x = pos[0] - src.get_width() // 2
            y = pos[1] - src.get_height() // 2
            surface.blit(src, (x, y))

    def draw_projectile(self, surface, proj):
        """Draw a moving projectile (heart or +5). proj is a dict with 'surf' and 'pos'."""
        s = proj.get('surf')
        if s:
            x = int(proj['pos'][0] - s.get_width() // 2)
            y = int(proj['pos'][1] - s.get_height() // 2)
            surface.blit(s, (x, y))

    def draw_boom(self, surface, pos, alpha=255):
        # draw boom text with optional alpha
        s = self.boom_surf.copy()
        s.fill((255, 255, 255, 0), None, pygame.BLEND_RGBA_MULT)
        if alpha < 255:
            s.set_alpha(alpha)
        x = int(pos[0] - s.get_width() // 2)
        y = int(pos[1] - s.get_height() // 2)
        surface.blit(s, (x, y))

    def draw_hole(self, surface, pos, scale=1.0):
        """在指定位置绘制洞口"""
        if scale != 1.0:
            w = int(self.hole_surface.get_width() * scale)
            h = int(self.hole_surface.get_height() * scale)
            scaled = pygame.transform.smoothscale(self.hole_surface, (w, h))
            x = pos[0] - w // 2
            y = pos[1] - h // 2
            surface.blit(scaled, (x, y))
        else:
            x = pos[0] - self.hole_surface.get_width() // 2
            y = pos[1] - self.hole_surface.get_height() // 2
            surface.blit(self.hole_surface, (x, y))
